package Log;

use strict;
use warnings;
use Exporter qw(import);

use POSIX qw(strftime);
use Utils qw(dd);
use App;
use Carp;

our (
    $level_error,
    $level_warn,
    $level_primary,
    $level_info,
    $level_debug
) = (
    0b00001,
    0b00010,
    0b00100,
    0b01000,
    0b10000
);
our $all = $level_error | $level_warn | $level_primary | $level_info | $level_debug;
my $level = $level_error | $level_warn | $level_primary | $level_info | $level_debug;

sub setLevel
{
    my $arg = shift;
    unless (defined $arg) {
        croak "Invalid argument";
    }
    $level = $arg;
}

sub primary 
{
    my $msg = shift;
    if ($level & $level_primary) {
        message("[\e[34mPRIMARY\e[0m]: $msg");
    }
}

sub info
{
    my $msg = shift;
    if ($level & $level_info) {
        message("[\e[32mINFO\e[0m]: $msg");
    }
}

sub warn
{
    my $msg = shift;
    if ($level & $level_warn) {
        message("[\e[33mWARN\e[0m]: $msg");
    }
}

sub error
{
    my $msg = shift;
    if ($level & $level_error) {
        message("[\e[31mERROR\e[0m]: $msg");
    }
}

sub debug
{
    my $msg = shift;
    if ($level & $level_debug) {
        message("[\e[37mDEBUG\e[0m]: $msg");
    }
}

sub message
{
    my $msg = shift;
    my $time = strftime("%Y-%m-%d %H:%M:%S", localtime);
    my $day = strftime("%Y%m%d", localtime);
    print "[${time}] [$$]: $msg\n";
    system("echo \"[${time}] [$$]: $msg\" >> $Const::LOG_PATH/${day}.log");
}

1;

__END__
